package com.example.photograph.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import org.springframework.util.CollectionUtils;

public class HttpUtil {
	public HttpUtil() {
	}

	public static void main(String[] args) {
		String url = "https://wap.fj.10086.cn/store/api/pe/me/base/info";
		System.out.println(refreshCookie("laravel_session=eyJpdiI6ImNjN3RFbno2MGFoQTNxUEttdEhxZ1E9PSIsInZhbHVlIjoiZnRjZVdhRkRxUlYrbldYcnFGK2JsY1hQTHJwRVB3emNXTEIwdWZWUFR6RGtaS0RrSHZQdTRFRjZxSlFTMnpMMlhvYW1Cc0tjMHBBSHZpMnBxQUNTSTB0Z1BtMnFFNGhBVnR0NFN2cVdxUE5RdHMyamQ5WXFUck9HWkk3d2U3M0QiLCJtYWMiOiI5NDgxNzA1NWM1Y2UzZWY3NjhjYTI3YjY4YTQwMDM0OTQxMWNhNTA0NjYyOTQ1YzI4Mjk0NDkxMmFjYTdjZjAwIn0;remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d=eyJpdiI6IkRiZUh0a3JPUlh1TXlMQ1U2akRWemc9PSIsInZhbHVlIjoiRkVaYTRpS1dPanphOTdJaG5PN3FIXC9SQklUTytcL3FwOWxCSDRjcmI0czFDVjBWOXp6S2lKU0g0Ym4zdDQ5RkpSc2xnUWlyRUQ5dXlmSkRtcVVUdWNhSUdJOG9pQUVvbXNkWWV1YXFTZUJmSWZVY3ZnaG8xVlJMemhhcURZSVNTSzk3VEtXbDl3M1pPdTRjMUJXQ0Q2dFE9PSIsIm1hYyI6ImE4OGJiNGFhN2JmMjYxY2ZjNjUwODFhODQzNTg1OGMyM2MyMjExOGE0YjNhMjhmOWQ3ODZlNTU1YjJkZWM3NTkifQ"));
	}

	public static String refreshCookie(String cookie) {
		String url = "https://wap.fj.10086.cn/store/api/pe/me/base/info";
		List<Cookie> cookies = sendGet(url, cookie);
		if (!CollectionUtils.isEmpty(cookies)) {
			List<Cookie> laravelessionList = (List)cookies.stream().filter((value) -> {
				return value.getName().equals("laravel_session");
			}).collect(Collectors.toList());
			if (!CollectionUtils.isEmpty(laravelessionList)) {
				Cookie laravelSession = (Cookie)laravelessionList.get(0);
				return laravelSession.getName() + "=" + laravelSession.getValue();
			}
		}

		return null;
	}

	public static List<Cookie> sendGet(String url, String cookie) {
		HttpGet httpget = new HttpGet(url);
		httpget.setHeader("Cookie", cookie);
		CloseableHttpResponse response = null;
		BasicCookieStore cookieStore = new BasicCookieStore();

		CloseableHttpClient httpclient;
		try {
			httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
			response = httpclient.execute(httpget);
		} catch (IOException var20) {
			return new ArrayList();
		}

		httpclient = null;

		List var8;
		try {
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String result = EntityUtils.toString(entity);
				JSONObject jsonObject = JSON.parseObject(result);
				if (jsonObject.get("status_code") != null && jsonObject.get("status_code").equals(401)) {
					ArrayList var26 = new ArrayList();
					return var26;
				}
			}

			List<Cookie> cookies = cookieStore.getCookies();
			var8 = cookies;
		} catch (IOException | ParseException var21) {
			ArrayList var7 = new ArrayList();
			return var7;
		} finally {
			try {
				response.close();
			} catch (IOException var19) {
				var19.printStackTrace();
			}

		}

		return var8;
	}

	public static List<Cookie> getLoginCookie(String url, Map<String, String> map) throws UnsupportedEncodingException {
		HttpPost httppost = new HttpPost(String.valueOf(url));
		CloseableHttpResponse response = null;
		CookieStore cookieStore = new BasicCookieStore();
		CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

		try {
			StringEntity entity = new StringEntity(JSON.toJSONString(map), ContentType.APPLICATION_JSON);
			httppost.setEntity(entity);
			httppost.setHeader("Content-Type", "application/json; charset=UTF-8");
			response = httpclient.execute(httppost);
		} catch (IOException var10) {
			var10.printStackTrace();
		}

		HttpEntity entity1 = response.getEntity();
		String result = null;

		try {
			result = EntityUtils.toString(entity1);
			return cookieStore.getCookies();
		} catch (IOException | ParseException var9) {
			var9.printStackTrace();
			return new ArrayList();
		}
	}

	public static String sendPost(String url, Map<String, String> map) {
		List<NameValuePair> formparams = new ArrayList();
		Iterator var3 = map.entrySet().iterator();

		while(var3.hasNext()) {
			Entry<String, String> entry = (Entry)var3.next();
			formparams.add(new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue()));
		}

		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
		HttpPost httppost = new HttpPost(url);
		httppost.setEntity(entity);
		CloseableHttpResponse response = null;

		try {
			CloseableHttpClient httpclient = HttpClients.custom().build();
			response = httpclient.execute(httppost);
		} catch (IOException var10) {
			var10.printStackTrace();
		}

		HttpEntity entity1 = response.getEntity();
		String result = null;

		try {
			result = EntityUtils.toString(entity1);
			return result;
		} catch (IOException | ParseException var9) {
			var9.printStackTrace();
			return result;
		}
	}

	public static String sendPost(String url) {
		return "result";
	}
}